/*
 *	This file is part of Lounge-Cast.
 *
 *  Author:  	Markus Moormann
 *	E-mail:  	mmoorman@uni-osnabrueck.de
 *	Copyright:	University of Osnabrueck (Zentrum Virtuos)
 * 	Website:	www.uni-osnabrueck.de
 *	Date:		16.02.2011
 *
 *	Lounge-Cast is free software: you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation, either version 3 of the License, or
 *  (at your option) any later version.
 *
 *	Lounge-Cast is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with Lounge-Cast.  If not, see <http://www.gnu.org/licenses/>.
 */
package multimedia.control.command.preview
{
	import com.adobe.cairngorm.commands.ICommand;
	import com.adobe.cairngorm.commands.SequenceCommand;
	import com.adobe.cairngorm.control.CairngormEvent;
	
	import flash.system.Capabilities;
	import flash.text.TextFormatAlign;
	
	import multimedia.control.event.preview.LoadVideoEvent;
	import multimedia.model.ModelLocator;
	import multimedia.model.ViewLocator;
	import multimedia.model.vo.CaptionSetVO;
	import multimedia.model.vo.CaptionVO;
	
	import mx.controls.Alert;
	import mx.core.Application;
	import mx.core.FlexGlobals;
	import mx.events.CloseEvent;
	import mx.managers.CursorManager;
	import mx.managers.PopUpManager;
	
	import org.osmf.elements.DurationElement;
	import org.osmf.elements.ParallelElement;
	import org.osmf.elements.SerialElement;
	import org.osmf.elements.TextElement;
	import org.osmf.elements.VideoElement;
	import org.osmf.events.LoadEvent;
	import org.osmf.layout.LayoutMetadata;
	import org.osmf.media.URLResource;
	import org.osmf.traits.AudioTrait;
	import org.osmf.traits.LoadState;
	import org.osmf.traits.LoadTrait;
	import org.osmf.traits.MediaTraitType;
	

	/**
	 * 	LoadVideoCommand
	 *
	 *	@author jemden
	 *	@version 1.0
	 */
	public class LoadVideoCommand extends SequenceCommand implements ICommand
	{
		private var model:ModelLocator;

		private var e:LoadVideoEvent;

		private var video:VideoElement;
		
		private var parallel:ParallelElement;
		
		private var captions:SerialElement; 
		
		private var loadable:LoadTrait;
		
		private var viewLocator:ViewLocator;

		override public function execute(event:CairngormEvent):void
		{
			this.model = ModelLocator.getInstance();
			this.viewLocator = ViewLocator.getInstance();
			this.e = LoadVideoEvent(event);
			this.nextEvent = CairngormEvent(e.nextEvent);
			trace("load VideoCommand - " + e.video.url);
			// Create the new media.
			video = new VideoElement(new URLResource(e.video.url));
			
			if(e.video.captionSets != null)
			{
				parallel = new ParallelElement();
				captions = new SerialElement();
				
				var captionSet:CaptionSetVO = e.video.captionSets.getItemAt(0) as CaptionSetVO;
				for(var i:int = 0; i < captionSet.captions.length; i++)
				{
					var caption:CaptionVO = captionSet.captions.getItemAt(i) as CaptionVO;
					if(i == 0 && caption.begin != 0)
					{
						//add buffer to first caption
						captions.addChild(new DurationElement(caption.begin,
											new TextElement("")));
						
					}
					
					//next caption
					captions.addChild(new DurationElement(caption.end - caption.begin,
										new TextElement(caption.text, true, TextFormatAlign.CENTER)));
					
					if(i + 1 < captionSet.captions.length &&
						caption.end != (captionSet.captions.getItemAt(i + 1) as CaptionVO).begin)
					{
						//add buffer to next caption
						captions.addChild(
							new DurationElement((captionSet.captions.getItemAt(i) as CaptionVO).begin - caption.end,
								new TextElement("")));
					}
				}
				
				
				
				
				var videoLayout:LayoutMetadata = new LayoutMetadata();
				video.addMetadata(LayoutMetadata.LAYOUT_NAMESPACE, videoLayout);
				videoLayout.percentHeight = 100;
				videoLayout.percentWidth = 100;
				parallel.addChild(video);
				
				var captionLayout:LayoutMetadata = new LayoutMetadata();
				captions.addMetadata(LayoutMetadata.LAYOUT_NAMESPACE, captionLayout);
				captionLayout.percentWidth = 100;
				captionLayout.percentY = 80;
				captionLayout.percentX = 0;
				parallel.addChild(captions);
				
				var parallelLayout:LayoutMetadata = new LayoutMetadata();
				parallel.addMetadata(LayoutMetadata.LAYOUT_NAMESPACE, parallelLayout);
				parallelLayout.width = Capabilities.screenResolutionX;
				parallelLayout.height = Capabilities.screenResolutionY;
				
			}
			CursorManager.setBusyCursor();
			
			loadable = video.getTrait(MediaTraitType.LOAD) as LoadTrait;
			loadable.addEventListener(LoadEvent.LOAD_STATE_CHANGE, onLoadableStateChange);
			loadable.load();
			
			executeNextCommand();
		}
		
		private function onLoadableStateChange(event:LoadEvent):void
		{
			CursorManager.removeBusyCursor();
			if(event.loadState == LoadState.READY)
			{
				trace("Video loaded");
				loadable.removeEventListener(LoadEvent.LOAD_STATE_CHANGE, onLoadableStateChange);
				
				var audible:AudioTrait = video.getTrait(MediaTraitType.AUDIO) as AudioTrait;
				audible.volume = e.video.volume;
				
				var height:int = FlexGlobals.topLevelApplication.stage.height * 0.9;
				var width:int = (e.video.aspect_ratio == "16:9" ? height * (16/9) : height * (4/3)); 
				
				//for resolution not 16/9
				//we need to start with the width
				if (width > FlexGlobals.topLevelApplication.stage.width)
				{
					width = FlexGlobals.topLevelApplication.stage.width * 0.9;
					height = (e.video.aspect_ratio == "16:9" ? width / (16/9) : width / (4/3));
				}
				
				model.wrapper.height = height;
				model.wrapper.width = width;
				model.wrapper.x = FlexGlobals.topLevelApplication.stage.width * 0.5 - width * 0.5;
				model.wrapper.y = FlexGlobals.topLevelApplication.stage.height * 0.5 - height * 0.5;
				
				if(e.video.captionSets != null)
				{
					model.wrapper.addMediaElement(parallel);
					model.wrapper.media = parallel;
				}else
				{
					model.wrapper.addMediaElement(video);
					model.wrapper.media = video;
				}
				
				model.wrapper.mediaPlayer.play();
			}else if(event.loadState == LoadState.LOAD_ERROR)
			{
				Alert.show("Verbindung zum Medium fehlgeschlagen. Vorschau wird nun beendet","Verbindung fehlgeschlagen",
					Alert.OK, null, 
					function close(event:CloseEvent):void
					{
						PopUpManager.removePopUp(viewLocator.preview);
					});
			}
			
		}
	}
}